﻿using System;
using System.Collections.Generic;
using System.Text;

namespace eKanBan.Model
{
    [Serializable]
    class Task : IEnumerable<Member>
    {
        private List<Member> doers;

        public IList<Member> Doers { get { return doers.AsReadOnly(); } }
        
        public enum StatusType
        {
            Ongoing,
            Completed,
            Blocked,
            Defect
        }

        public Task(Feature feature)
        {
            doers = new List<Member>();
            StartTime = DateTime.Now;
            FinishTime = DateTime.MinValue;
            Feature = feature;
        }

        #region Properties
        public Feature Feature { get; set; }
        public string Description { get; set; }
        public int Priority { get; set; }
        public StatusType Status { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime FinishTime { get; set; }
        public DateTime DeadLine { get; set; }
        public bool Done { get { return Status == StatusType.Completed; } }

        public bool OnGoing { get { return Status == StatusType.Ongoing; } }
        #endregion

        public IEnumerator<Member> GetEnumerator()
        {
            return doers.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return doers.GetEnumerator();
        }

        public void AddNewMember(Member member) 
        {
            if (!doers.Contains(member))
            {                
                doers.Add(member);
                member.Take(this);
            }
        }

        public void RemoveMember(Member member)
        {
            if (doers.Contains(member))
            {
                doers.Remove(member);
                //set finish time of this task
                member.IsDoing = false;
                member.CurTaskFinishTime = DateTime.Now;
            }
        }

        public override string ToString()
        {
            string str = String.Format("Description: {0}\nPriority: {1}\tStatus: {2}\nStartTime: {3}\tFinishTime: {4}\tDeadline: {5}", Description, Priority, Status, StartTime, FinishTime, DeadLine);
            str += "\nDoers:";
            foreach (Member item in doers)
            {
                str += String.Format("\n\tName: {0}", item.Name);
            }
            return str;
        }
    }
}
